package cn.tedu.property.management.service.impl;

import cn.tedu.property.management.ex.ServiceException;
import cn.tedu.property.management.mapper.RepairerMapper;
import cn.tedu.property.management.pojo.dto.RepairAddNewDTO;
import cn.tedu.property.management.pojo.dto.RepairUpdateDTO;
import cn.tedu.property.management.pojo.vo.RepairListItemVO;
import cn.tedu.property.management.pojo.entity.Repairer;
import cn.tedu.property.management.pojo.vo.RepairStandardVO;
import cn.tedu.property.management.service.IRepairerService;
import cn.tedu.property.management.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import java.util.List;


/**
 * 管理维修人员的业务类
 */
@Service
@Slf4j
public class RepairerServiceImpl implements IRepairerService {

    @Autowired
    private RepairerMapper repairerMapper;


    @Override
    public void addNew(RepairAddNewDTO repairAddNewDTO) {
        log.info("开始处理添加维修人员的业务");
        RepairStandardVO standard = repairerMapper.getStandardByName(repairAddNewDTO.getName());
        if ( standard!=null ){
            throw new ServiceException(ServiceCode.ERR_UNKNOWN,"新增失败,该师傅已经添加过了");
        }
        Repairer repairer = new Repairer();
        BeanUtils.copyProperties(repairAddNewDTO,repairer);
        repairerMapper.addNew(repairer);
    }

    @Override
    public void deleteById(Long id) {
        log.info("开始处理删除维修人员的业务");
        RepairStandardVO standard = repairerMapper.getStandardById(id);
        if ( standard ==null ){
            throw new ServiceException(ServiceCode.ERR_UNKNOWN,"请求异常,尝试删除的维修人员不存在");
        }
        repairerMapper.deleteById(id);
    }

    @Override
    public void update(Long id, RepairUpdateDTO repairUpdateDTO) {
        log.info("开始处理修改维修人员信息的业务");
        RepairStandardVO standard = repairerMapper.getStandardById(id);
        if (standard == null) {
            throw new ServiceException(ServiceCode.ERR_UNKNOWN,"请求异常,该维修人员不存在");
        }
        Repairer repairer = new Repairer();
        BeanUtils.copyProperties(repairUpdateDTO,repairer);
        repairer.setId(id);
        repairerMapper.update(repairer);
    }

    @Override
    public List<RepairListItemVO> getAllRepairs() {
        log.info("开始处理查询维修人员信息的业务");
        List<RepairListItemVO> repairs = repairerMapper.getAllRepairs();
        return repairs;
    }
}
